class Solution {
public:
    typedef unsigned int uint;
    int atoi(const char *str) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (str == NULL) {
            return 0;
        }
        uint sign = 0;
        uint maxInt = numeric_limits<int>::max();

        while (*str && *str == ' ') {
            ++str;
        }

        if (!(*str == '-' || *str == '+' || (*str >= '0' && *str <= '9'))) {
            return 0;
        }
        if (*str == '-' || *str == '+') {
            if (*str == '-') {
                sign = 1;
            }
            ++str;
        }

        uint sum = 0;
        while (*str && *str >= '0' && *str <= '9') {
            uint ch = *str - '0';
            if (sum > (maxInt + sign - ch) / 10) {
                return sign ? numeric_limits<int>::min() : numeric_limits<int>::max();
            }
            sum = sum * 10 + ch;
            ++str;
        }
        return sign ? -sum : sum;
    }
};
